<?php

class SetWelcomeMessageAction extends CAction
{
    public function run()
    {
        if(!isset($_POST['value']) || !isset($_GET['id']))
            throw new CHttpException(400,'the request is invalid');

        if($_GET['id']!=Yii::app()->user->id)
            throw new CHttpException(403,'cannot edit the kitchen not belong to you');

        $kitchen=Kitchen::loadObjectById($_GET['id']);
        if($kitchen===null)
            throw new CHttpException(404,'The requested kitchen does not exist.');
        if($kitchen->id!=Yii::app()->user->id)
            throw new CHttpException(403,'cannot edit the kitchen not belong to you');

        $succ=false;
        if($_POST['value']===$kitchen->welcomeMessage){
            $succ=true;
        }
        else {                 
            $oldWelcomeMessage=$kitchen->welcomeMessage;                
            $kitchen->welcomeMessage=$_POST['value'];
            $succ=Kitchen::saveObject($kitchen,array('welcomeMessage','updateTime'));
            if(!$succ)
                $kitchen->welcomeMessage=$oldWelcomeMessage;
        }

        $this->controller->renderPartial('/common/editInPlace',array(
            'succ'=>$succ, 
            'data'=>$kitchen->welcomeMessage,
            'error'=>$kitchen->getError('welcomeMessage'),
        ));
    }
}
  
?>
